// page/searchresult/result.js
var config = require('../../config.js');
var common = require('../utils/common.js');
var loadSearchResult = require('loadSearchResult.js');
var app = getApp();
var username = '';


var tab_page = {};//视频结果存放
var page = 0;
var size = 7;
var key='';
var inputVal='';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keywords: '',
    material_list: [],
    series_list: [],
    total_list:0,
    total_series:0,
    lastLoadTime: new Date().getTime(),
    windowHeight: 0,
    clickId: 0,//判断重复点击
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var keywords = options.keywords;
    inputVal = keywords;
    tab_page={};
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowHeight: res.windowHeight - 30,
          keywords: keywords
        })
      }
    });
    var data = {
      page: 0,
      size: size,
      keywords: keywords
    };
    loadSearchResult.search_materials_page(data, tab_page, username, that);
    loadSearchResult.load_series(keywords, that);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: config.shareDes,
      path: '/pages/index/index',
      imageUrl: '../../images/sharepic_1.jpg'
    }
    // return {
    //   title: config.shareName,
    //   desc: config.shareDes,
    //   path: '/pages/searchlist/searchlist?keywords=' + inputVal
    // }
  }
  , search_input: function (e) {
    var that = this;
    var keywords = e.detail.value;

    inputVal = keywords;
    that.setData({
      keywords: keywords
    });
    tab_page = {};
    if(!keywords){
      that.setData({
        material_list: [],
        series_list: []
      });
    }
    var data = {
      page: 0,
      size: size,
      keywords: keywords
    };

    loadSearchResult.search_materials_page(data, tab_page, username, that);
    loadSearchResult.load_series(keywords, that);

  }, more_materials: function (e) {//更多视频
    var keywords = e.currentTarget.dataset.keywords;
    wx.redirectTo({
      url: 'material/index?keywords=' + keywords
    });
  }, more_series: function (e) {//更多系列
    var keywords = e.currentTarget.dataset.keywords;
    wx.redirectTo({
      url: 'series/index?keywords=' + keywords
    });
  }, inplay_material_p: function (e) {//进入播放页面 父页面
    var title = e.currentTarget.dataset.title;
    var id = e.currentTarget.dataset.id;
    if (id == 0 || id == null) {
      return;
    }

    if (this.data.clickId==id){
        return;
    }
    this.setData({
      clickId: id
    });
    var url = '../video/video?id=' + id + '&title=' + title;
    wx.navigateTo({
      url: url,
    })
  }, inplay_series: function (e) {
    var id = e.currentTarget.dataset.id;
    if(id==null||id==0){
      return;
    }
    wx.navigateTo({
      url: '../search/seriesList/serieslist?id=' + id
    })
  }, favor: function (e) {//收藏或取消
    var that = this;
    var idd = e.currentTarget.dataset.id;
    var fid = e.currentTarget.dataset.fid;//是否收藏1 是，0 否
    if(idd==null||idd==0){
      return;
    }

    if (fid == '1') {//表示取消收藏   
      common.add_rem_favorite(idd, username, 'remove',
        common.change_tab_list(idd, '0', that.data, that)
      );
    } else {//表示需要收藏
      common.add_rem_favorite(idd, username, 'add',
        common.change_tab_list(idd, '1', that.data, that)
      );
    }

  },loadMore: function () {
    var that=this;
    if (tab_page.page == undefined) {
      return;
    }
    var currentPage = tab_page.page.number + 1;
    if (currentPage <= tab_page.page.totalPages - 1) {
      var curTime = new Date().getTime();
      //上一次加载的时间
      var lastTime = this.data.lastLoadTime;
      that.setData({
        lastLoadTime: curTime
      });
      if (curTime - lastTime < 300) {
        return;
      }
      wx.showLoading({
        title: '加载中···',
      });
      var data = {
        page: currentPage,
        size: size,
        keywords: tab_page.keywords
      };
     

      loadSearchResult.search_materials_page(data, tab_page, username, that);
 
    }
    var timeout = setTimeout(function () {
      wx.hideLoading();
      clearTimeout(timeout);
    }, 500);
  }, refesh:function(e){}
})